<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :show="show" @click-show="show = !show" :fixed="true" :back="true" color="#fff" title="待我审批"></uni-navbar></view>

		<view class="filter-box" v-if="show">
			<view class="form-box">
				<u-form ref="uForm">
					<u-form-item class="form-item" label="类型">
						<u-input v-model="type" type="select" :select-open="typeShow" placeholder="请选择类型" @click="typeShow = true" />
						<u-select v-model="typeShow" :list="typeList" @confirm="typeConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="状态">
						<u-input v-model="kind" type="select" :select-open="kindShow" placeholder="请选择类型" @click="kindShow = true" />
						<u-select v-model="kindShow" :list="kindList" @confirm="kindConfirm"></u-select>
					</u-form-item>
					<u-form-item class="form-item" label="开始时间">
						<date-time-picker placeholder="请选择开始时间" :defaultValue="startTime" :old="true" fields="day" @change="timeChange($event, 'startTime')"></date-time-picker>
					</u-form-item>
					<u-form-item class="form-item" label="结束时间">
						<date-time-picker placeholder="请选择结束时间" :defaultValue="endTime" :old="true" fields="day" @change="timeChange($event, 'endTime')"></date-time-picker>
					</u-form-item>
				</u-form>
			</view>
			<view class="fiexd-btm">
				<view class="flex-box">
					<u-button class="form-btn" type="default" @click="resert">清除</u-button>
					<u-button class="form-btn" type="error" @click="filter">确定</u-button>
				</view>
			</view>
		</view>
		<view class="search-view">
			<u-search class="search-input" shape="square" bg-color="#fff" v-model="dispose.title" :show-filter="true" @search="search" @custom="show = !show"></u-search>
		</view>
		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#f5f7fa' }">
			<view class="list-info_04" v-for="(item, index) of infoList" :key="item.id" @click="navTo(item)">
				<view class="info-img"><image class="sub-img" :src="$imagePreview(item.logo)" mode="aspectFill"></image></view>
				<view class="info-btm">
					<view class="left-title clamp">{{ item.userName }}【{{ item.vacateTypeLabel }}申请】</view>
					<view class="left-time clamp">{{ item.userJob }} | {{ item.createTime | parseTime('{y}-{m}-{d}') }}</view>
				</view>
				<view class="right-type" :style="'color: ' + colorMap[item.approvalStatus]">{{ item.approvalStatusLabel }}</view>
			</view>
			<u-loadmore :status="loading" />
		</view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>

		<view v-if="false && dispose"></view>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker
	},
	data() {
		return {
			scrollTop: 0,
			show: false,
			tabsCurrent: 0,
			tabsList: [],

			type: '',
			typeShow: false,
			typeList: [
				{
					value: 'vacate',
					label: '请假'
				},
				{
					value: 'evection',
					label: '出差'
				},
				{
					value: 'dinning',
					label: '用餐'
				}
			],

			kind: '',
			kindShow: false,
			kindList: [],

			colorMap: {
				0: '#303133',
				1: '#f29100',
				2: '#18b566',
				3: '#dd6161',
				4: '#0099ff',
				5: '#ff9900',
				6: '#19be6b',
				7: '#ff3333',
				8: '#909399'
			},

			startTime: '',
			endTime: '',
			infoList: [],

			dispose: {
				title: '',
				disposeUser: '',
				approvalStatus: '',
				serverType: '',
				clientCode: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad(option) {
		this.loadLogin();
		this.loadDict('oa_attnd_status', 'kindList');

		this.loadData();
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		dateRangeSea: {
			get: function () {
				let time = this.startTime + ', ' + this.endTime;
				if (this.startTime == '' || this.endTime == '') {
					return '';
				} else {
					return time;
				}
			},
			set: function (e) {
				this.startTime = '';
				this.endTime = '';
			}
		},
		...mapState(['userInfo'])
	},
	methods: {
		typeConfirm(e) {
			this.type = e[0].label;
			this.dispose.serverType = e[0].value;
		},
		kindConfirm(e) {
			this.kind = e[0].label;
			this.dispose.approvalStatus = e[0].value;
		},
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.dispose.clientCode = this.userInfo.sysUser.clientCode;
				this.dispose.disposeUser = this.userInfo.sysUser.id;

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				this.$logout();
			}
		},
		loadData(type = 'add') {
			if (this.loading == 'loading') {
				return false;
			}
			if (type == 'add') {
				if (this.loading == 'nomore') {
					return false;
				}
				this.loading = 'loading';
			} else {
				this.loading = 'loading';
			}
			if (type != 'add') {
				this.pageNum = 1;
			}
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				dispose: this.dispose
			};
			this.$request('/attndVacate/oa-attnd-vacate/disposeList', data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.infoList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.infoList = this.infoList.concat(res.data.records);
				this.loaded = true;
				if (this.infoList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		loadDict(type, list) {
			this.$request('/system/sys-dict/dictList', {
				clientCode: this.clientCode,
				sysDict: {
					dictType: type
				}
			}).then((res) => {
				if (!res.data) {
					return false;
				}
				this[list] = [];
				res.data.forEach((item) => {
					this[list].push({
						value: item.dictValue,
						label: item.dictName
					});
				});
			});
		},
		timeChange({ date, dt }, idx) {
			this[idx] = date.year + '-' + date.month2 + '-' + date.day2;
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.change(this.tabsList[index].value);
		},
		change(e) {
			this.type = e;
			this.dispose.type = e.toString();
			this.loadData('refresh');
		},
		filter() {
			if (this.startTime == '' && this.endTime != '') {
				this.$msg('请选择开始时间');
				return false;
			}
			if (this.startTime != '' && this.endTime == '') {
				this.$msg('请选择结束时间');
				return false;
			}
			let arr = this.startTime.split('-');
			let startTime = new Date(arr[0], arr[1], arr[2]).getTime();

			let arrs = this.endTime.split('-');
			let endTime = new Date(arrs[0], arrs[1], arrs[2]).getTime();

			if (startTime >= endTime) {
				this.$msg('开始时间应小于结束时间');
				return false;
			}

			this.show = false;
			this.dispose.createTimeArray = this.dateRangeSea;
			this.loadData('refresh');
		},
		resert() {
			this.show = false;
			this.type = '';
			this.dispose.serverType = '';
			this.kind = '';
			this.dispose.approvalStatus = '';
			this.dateRangeSea = '';
			this.dispose.createTimeArray = '';
			this.loadData('refresh');
		},
		search(e) {
			this.dispose.title = e;
			this.loadData('refresh');
		},
		navTo(item) {
			let url = '/pagesB/oa/detail_no1?id=';
			switch (item.serverType) {
				case 'vacate':
					url = '/pagesB/oa/detail_no1?id=';
					break;
				case 'evection':
					url = '/pagesB/oa/detail_no2?id=';
					break;
				case 'dinning':
					url = '/pagesB/oa/detail_no3?id=';
					break;
			}
			uni.navigateTo({
				url: url + item.id + '&disposeId=' + item.disposeId
			});
		},
		addTo() {
			uni.navigateTo({
				url: '/pagesB/oa/add_todo'
			});
		}
	}
};
</script>

<style lang="scss">
.uTabs {
	min-height: 88rpx;
}

.list-box {
	padding: 0;
	background-color: #fff;
}

.filter-box {
	position: fixed;
	left: 0;
	top: 0;
	bottom: 0;
	right: 0;
	z-index: 998;
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
